//
//  XD_BlankPageView.swift
//  XueDao
//
//  Created by wenwen on 2021/3/4.
//

import Foundation

// 定义block、传任意类型，无返回值
typealias btnActionBlock = () -> ()

class XD_BlankPageView: UIView {
    
    var btnActionBlock: btnActionBlock?
    
    lazy var imgView: UIImageView = {
        let imgView = UIImageView()
        imgView.contentMode = .center
        return imgView
    }()
    
    lazy var detailLab: UILabel = {
        let detailLab = UILabel()
        detailLab.font = .systemFont(ofSize: .scale*18)
        detailLab.textColor = UIColor(red: 0.45, green: 0.47, blue: 0.49, alpha: 1.0)
        detailLab.textAlignment = .center
        detailLab.numberOfLines = 1
        
        return detailLab
    }()
    
    lazy var actionBtn: UIButton = {
        let actionBtn = UIButton(type: .custom)
        actionBtn.titleLabel?.font = .xsFont(name: .medium, .scale * 16)
        actionBtn.setTitleColor(.white, for: .normal)
        actionBtn.backgroundColor = UIColor(red: 0.16, green: 0.74, blue: 0.61, alpha: 1.0)
        actionBtn.clipsToBounds = true
        actionBtn.layer.cornerRadius = .scale * 24
        
        actionBtn.addTarget(self, action: #selector(actionBtnClick), for: .touchUpInside)
        return actionBtn
    }()
    
    lazy var bottomDetailLab: UILabel = {
        let bottomDetailLab = UILabel()
        bottomDetailLab.font = .systemFont(ofSize: .scale*14)
        bottomDetailLab.textColor = UIColor(red: 0.45, green: 0.47, blue: 0.49, alpha: 0.6)
        bottomDetailLab.textAlignment = .center
        bottomDetailLab.numberOfLines = 1
        
        return bottomDetailLab
    }()
    
    @objc func actionBtnClick() {
        if let btnAction = self.btnActionBlock {
            btnAction()
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = UIColor(red: 0.97, green: 0.97, blue: 0.97, alpha: 1.0)
        addSubview(imgView)
        imgView.snp.makeConstraints { (make) in
            make.top.equalTo(.scale * 88)
            make.centerX.equalToSuperview()
        }
        
        addSubview(detailLab)
        detailLab.snp.makeConstraints { (make) in
            make.top.equalTo(imgView.snp.bottom).offset(.scale * 10)
            make.left.right.equalToSuperview()
            make.height.equalTo(.scale * 24)
        }
        
        addSubview(actionBtn)
        actionBtn.snp.makeConstraints { (make) in
            make.top.equalTo(detailLab.snp.bottom).offset(.scale * 56)
            make.width.equalTo(.scale * 215)
            make.height.equalTo(.scale * 48)
            make.centerX.equalToSuperview()
        }
        
        addSubview(bottomDetailLab)
        bottomDetailLab.snp.makeConstraints { (make) in
            make.left.right.equalToSuperview()
            make.height.equalTo(.scale * 24)
            make.bottom.equalTo(-.scale * 20)
        }
    }
    
    /// 隐藏删除空白页
    /// - Parameter superView: 父视图
    class func hiddenBlankPageView(superView: UIView) {
        for subV in superView.subviews {
            if subV.isKind(of: XD_BlankPageView.self) {
                subV.isHidden = true
                subV.removeFromSuperview()
            }
        }
    }
    
    /// 空白页
    /// - Parameters:
    ///   - superView: 父试图
    ///   - imgName: 图片名称
    ///   - detailStr: 无内容提示
    ///   - btnTitle: 按钮title
    ///   - bottomDetailStr: 底部提示
    ///   - btnAction: 按钮点击事件回调
    class func blankPage(superView: UIView, imgName: String, detailStr: String, btnTitle: String, bottomDetailStr: String, btnAction: @escaping btnActionBlock) {
        let blankPageView = XD_BlankPageView.init(frame: superView.frame)
        hiddenBlankPageView(superView: superView)
        superView.addSubview(blankPageView)

        blankPageView.blankPage(imgName: imgName, detailStr: detailStr, btnTitle: btnTitle, bottomDetailStr: bottomDetailStr, btnAction: btnAction)
    }
    
    /// 空白页
    /// - Parameters:
    ///   - superView: 父试图
    ///   - imgName: 图片名称
    ///   - detailStr: 无内容提示
    ///   - bottomDetailStr: 底部提示
    class func blankPage(superView: UIView, imgName: String, detailStr: String, bottomDetailStr: String) {
        let blankPageView = XD_BlankPageView.init(frame: superView.frame)
        hiddenBlankPageView(superView: superView)
        superView.addSubview(blankPageView)
        
        blankPageView.blankPage(imgName: imgName, detailStr: detailStr, bottomDetailStr: bottomDetailStr)
    }
    
    /// 空白页
    /// - Parameters:
    ///   - imgName: 图片名称
    ///   - detailStr: 无内容提示
    ///   - btnTitle: 按钮title
    ///   - bottomDetailStr: 底部提示
    ///   - btnAction: 按钮点击事件回调
    func blankPage(imgName: String, detailStr: String, btnTitle: String, bottomDetailStr: String, btnAction: @escaping btnActionBlock) {
        initBlankPages(imgName: imgName, detailStr: detailStr, btnTitle: btnTitle, bottomDetailStr: bottomDetailStr)
        btnActionBlock = btnAction
        actionBtn.isHidden = false
    }
    
    /// 空白页
    /// - Parameters:
    ///   - imgName: 图片名称
    ///   - detailStr: 无内容提示
    ///   - bottomDetailStr: 底部提示
    func blankPage(imgName: String, detailStr: String, bottomDetailStr: String) {
        initBlankPages(imgName: imgName, detailStr: detailStr, btnTitle: "", bottomDetailStr: bottomDetailStr)
        actionBtn.isHidden = true
    }
    
    /// 空白页
    /// - Parameters:
    ///   - imgName: 图片名称
    ///   - detailStr: 无内容提示
    ///   - btnTitle: 按钮title
    ///   - bottomDetailStr: 底部提示
    private func initBlankPages(imgName: String, detailStr: String, btnTitle: String, bottomDetailStr: String) {
        imgView.image = UIImage.init(named: imgName)
        detailLab.text = detailStr
        actionBtn.setTitle(btnTitle, for: .normal)
        bottomDetailLab.text = bottomDetailStr
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        super.touchesBegan(touches, with: event)
        
        xd_Window()?.endEditing(true)
    }
    
    private func xd_Window() -> UIWindow? {
        guard let delegate = UIApplication.shared.delegate, let window = delegate.window, window != nil else {
            return UIApplication.shared.keyWindow
        }
        return window
    }
}
